package com.medicine.manage.block;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class EncryptUtils {

    public static String sha256(String filePath) {
        String cipherString = null;
        InputStream inputStream = null;
        try {
            // 获取实例（SHA-512同理）
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            inputStream = new URL(filePath).openStream();
            byte[] buffer = new byte[1024];
            int len;
            while( (len = inputStream.read(buffer)) != -1 ) {
                messageDigest.update(buffer, 0, len);
            }
            byte[] digest = messageDigest.digest();

            // 输出为16进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            cipherString = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
        return cipherString;
    }
}
